﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.Globalization;
using Microsoft.Build.Framework;

namespace Ghost.Tasks.Hg.Command
{
    /// <summary>
    /// Implements the <see cref="IParameterBuilder"/> interface for
    /// the task items properties.
    /// </summary>
    internal class ItemsParameterBuilder : IParameterBuilder
    {
        /// <summary>
        /// Checks whether the managed property is an array of
        /// <see cref="ITaskItem"/>.
        /// </summary>
        /// <param name="propertyType">
        /// The checked property.
        /// </param>
        /// <returns>
        /// <c>true</c> if <paramref name="propertyType"/> is
        /// an array of <see cref="ITaskItem"/>, <c>false</c> otherwise.
        /// </returns>
        public bool CanBuild(Type propertyType)
        {
            return typeof(ITaskItem[]).IsAssignableFrom(propertyType);
        }

        /// <summary>
        /// Builds the parameter associated with an array of task item.
        /// </summary>
        /// <param name="parameterPrefix">
        /// The prefix used for the parameter. If empty, the builded
        /// parameter is the default parameter.
        /// </param>
        /// <param name="propertyType">
        /// The type of the property.
        /// </param>
        /// <param name="propertyValue">
        /// The current value of the property. Can be <c>null</c>.
        /// </param>
        /// <returns>
        /// The string used to represent the current property in the
        /// Mercurial command.
        /// </returns>
        /// <exception cref="NotSupportedException">
        /// The <paramref name="propertyType"/> is not an array of task item.
        /// </exception>
        public string Build(string parameterPrefix, Type propertyType, object propertyValue)
        {
            if (!this.CanBuild(propertyType))
            {
                throw new NotSupportedException(Error.ParameterBuilderCantBuild);
            }

            ITaskItem[] items = propertyValue as ITaskItem[];
            if (items != null && items.Length > 0)
            {
                string[] values = new string[items.Length];

                for (int i = 0; i < items.Length; i++)
                {
                    ITaskItem item = items[i];
                    string value = StringParameterBuilder.Escape(item.ItemSpec);

                    if (string.IsNullOrEmpty(parameterPrefix))
                    {
                        values[i] = value;
                    }
                    else
                    {
                        values[i] = string.Format(CultureInfo.InvariantCulture, "{0} {1}", parameterPrefix, value);
                    }
                }

                return string.Join(" ", values);
            }
            else
            {
                return null;
            }
        }
    }
}
